﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpiceNet.Models;

namespace SpiceNet.Circuits
{
    /// <summary>
    /// Represents a circuit - Handles all models
    /// </summary>
    public partial class Circuit
    {
        /// <summary>
        /// Private variables
        /// </summary>
        private Dictionary<Type, List<Model>> models = new Dictionary<Type, List<Model>>();
        private Dictionary<Type, Model> default_model = new Dictionary<Type, Model>();

        /// <summary>
        /// Add a model to the circuit
        /// </summary>
        /// <param name="model">One</param>
        public void Add(params Model[] mlist)
        {
            for (int i = 0; i < mlist.Length; i++)
            {
                // Get the type of the model
                Type type = mlist[i].GetType();

                if (!models.ContainsKey(type))
                    models.Add(type, new List<Model>());
                if (!models[type].Contains(mlist[i]))
                    models[type].Add(mlist[i]);
            }
        }

        /// <summary>
        /// Add an instance to the list
        /// </summary>
        /// <param name="ilist"></param>
        public void Add(params Instance[] ilist)
        {
            for (int i = 0; i < ilist.Length; i++)
            {
                Instance inst = ilist[i];
                if (!default_model.ContainsKey(inst.ModelType))
                {
                    Model newmodel = (Model)Activator.CreateInstance(inst.ModelType, "");
                    default_model.Add(inst.ModelType, newmodel);
                    if (!models.ContainsKey(inst.ModelType))
                        models.Add(inst.ModelType, new List<Model>());
                    models[inst.ModelType].Add(newmodel);
                }
                default_model[inst.ModelType].Add(inst);
            }
        }

        /// <summary>
        /// Remove a model from the circuit
        /// </summary>
        /// <param name="model"></param>
        public void RemoveModel(Model model)
        {
            // Get the type of the model
            Type type = model.GetType();

            // Delete the model from the dictionary
            if (!models.ContainsKey(type))
                return;
            models[type].Remove(model);
        }

        /// <summary>
        /// Clear all models from the circuit
        /// </summary>
        public void ClearModels()
        {
            // Clear all models
            models.Clear();
        }

        /// <summary>
        /// Clear instances for each model and then clear the list of models
        /// </summary>
        public void Clear()
        {
            // Clear each model individually
            foreach (Type type in models.Keys)
            {
                for (int i = 0; i < models[type].Count; i++)
                    models[type][i].Clear();
            }

            // Finally clear all models
            models.Clear();
        }
    }
}
